package com.java.userservice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.userservice.dao.PermissionMapper;
import com.java.userservice.model.Permission;
import com.java.userservice.model.vo.PermissionVo;
import com.java.userservice.service.PermissionService;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public Result save(Permission permission) {
        permissionMapper.save(permission);
        return Result.success();
    }

    @Override
    public Result modify(Permission permission) {
        permissionMapper.modify(permission);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            permissionMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(Permission permission) {
        PageHelper.startPage(permission.getPageNum(), permission.getPageSize()); // 提醒page在显示哪一页的数据和要显示多少条数据

        List<Permission> list = permissionMapper.list(permission);
        PageInfo<Permission> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        permissionMapper.get(id);
        return Result.success();
    }

    @Override
    public List<PermissionVo> listByLoginId(Integer adminId) {
        List<PermissionVo> list = new ArrayList<>();

        List<Permission> permissions = permissionMapper.listByLoginId(adminId);
        Set<Integer> ids = permissions.stream().map(Permission::getId).collect(Collectors.toSet());

        if (!CollectionUtils.isEmpty(permissions)) {
            List<Integer> parentIds = permissions.stream().map(Permission::getParentId).collect(Collectors.toList());
            // 第一级菜单
            for (Permission permission : permissions) {
                for (Integer parentId:parentIds){
                    if(permission.getId().equals(parentId)){
                        if(ids.contains(permission.getId())){
                            if (permission.getParentId() == null) {
                                PermissionVo permissionVo = new PermissionVo();
                                permissionVo.setId(permission.getId());
                                permissionVo.setAuthName(permission.getAuthName());
                                permissionVo.setPath(permission.getPath());
                                list.add(permissionVo);
                                ids.remove(permission.getId());
                            }
                        }
                    }
                }
            }
            Map<Integer, PermissionVo> parentMap = list.stream()
                    .collect(Collectors.toMap(permissionVo -> permissionVo.getId(), permissionVo -> permissionVo));

            // 第二级菜单
            for (Permission permission : permissions) {
                if (permission.getParentId() != null) {
                    PermissionVo permissionVo = new PermissionVo();
                    permissionVo.setId(permission.getId());
                    permissionVo.setAuthName(permission.getAuthName());
                    permissionVo.setPath(permission.getPath());
                    parentMap.get(permission.getParentId()).getChildren().add(permissionVo);
                }
            }
        }

        return list;
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.listAll();
    }
}
